输入一串随机数字,然后按千分位输出。
比如输入数字串为“123456789”,输出为123,456,789
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| #!/bin/bash read -p "输入一串数字:" num v=`echo $num|sed 's/[0-9]//g'` if [ -n "$v" ] then echo "请输入纯数字." exit fi length=${#num} len=0 sum='' for i in $(seq 1 $length) do len=$[$len+1] if [[ $len == 3 ]] then sum=','${num:$[0-$i]:1}$sum len=0 else sum=${num:$[0-$i]:1}$sum fi done if [[ -n $(echo $sum | grep '^,' ) ]] then echo ${sum:1} else echo $sum fi 上面这个答案比较复杂,下面再来一个sed的 #!/bin/bash read -p "输入一串数字:" num v=`echo $num|sed 's/[0-9]//g'` if [ -n "$v" ] then echo "请输入纯数字." exit fi echo $num|sed -r '{:number;s/([0-9]+)([0-9]{3})/\1,\2/;t number}'
|